package com.tanhua.server.service;

import com.tanhua.domain.mongo.Exam;
import com.tanhua.domain.mongo.Level;
import com.tanhua.domain.mongo.Options;
import com.tanhua.domain.mongo.Questions;
import com.tanhua.domain.vo.ExamVo;
import com.tanhua.domain.vo.OptionVo;
import com.tanhua.domain.vo.QuestionVo;
import com.tanhua.dubbo.api.mongo.SoulApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Alfred
 * @Date: 2021/04/20/17:42
 * @Description:
 */
@Service
public class SoulService {

    @Reference
    private SoulApi soulApi;

    public List<ExamVo> testSoul() {
        //获取登录用户的id
        Long userLoginId = UserHolder.getUserId();

        List<Exam> examList=soulApi.getList();

        List<ExamVo> examVoList = new ArrayList<>();
        for (Exam exam : examList) {
            ExamVo examVo = new ExamVo();
            BeanUtils.copyProperties(exam,examVo);
            examVo.setId(exam.getExamId());

            //获得每张问卷对应的试题集合
            List<Questions> questions = soulApi.getQuestionList(examVo.getId());

            List<QuestionVo> questionVos = new ArrayList<>();


            for (Questions question : questions) {
                QuestionVo questionVo = new QuestionVo();

                BeanUtils.copyProperties(question,questionVo);
                questionVo.setId(question.getQuestion_Id());
                String question_id = question.getQuestion_Id();
                List<Options> list =  soulApi.getOptionList(question_id);
                List<OptionVo> optionVos = new ArrayList<>();
                for (Options option : list) {
                    OptionVo optionVo = new OptionVo();
                    optionVo.setId(option.getOption_id());
                    optionVo.setOption(option.getOption());
                    optionVos.add(optionVo);
                }


                questionVo.setOptions(optionVos);

                questionVos.add(questionVo);
            }

            examVo.setQuestions(questionVos);

            //获取用户对应的等级,判断是否解锁
            Level userLevel = soulApi.getUserLevel(userLoginId);
            Long level_user = userLevel.getLevel();
            long level_exam = Long.valueOf(exam.getLevel());
            if (level_user>=level_exam){
                examVo.setIsLock(0);
            }else {
                examVo.setIsLock(1);
            }

            examVo.setLevel("初级");
            //获取对应的报告的id
            String reportId=soulApi.getReportId(exam.getExamId());
            if (reportId!=null){
                examVo.setReportId(reportId);
            }
            examVo.setStar(Integer.parseInt(exam.getStar()));
            examVoList.add(examVo);
        }
        return examVoList;
    }
}
